//
//  BHCompetitionImageOrVideoCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/20.
//

import UIKit

class BHCompetitionImageOrVideoCell: UITableViewCell {
    
    @IBOutlet weak var content_label: UILabel!
    
    @IBOutlet weak var time_label: UILabel!
    
    
    @IBOutlet var imageViewArray: [UIImageView]!
    
    @IBOutlet var playBtnArray: [UIButton]!
    
    var model: BHCompetitionDynModel? {
        didSet {
            if model == nil {
                return
            }
            
            self.content_label.attributedText = model!.showTextAttribute
            self.time_label.text = model!.createTime
            
            if model!.sourceArray.count == 0 {
                for img in self.imageViewArray {
                    img.isHidden = true
                }
            }else {
                for (index,imgView) in self.imageViewArray.enumerated() {
                    let playBtn = self.playBtnArray[index]
                    if index < model!.sourceArray.count {
                        let sourceModel = model!.sourceArray[index]
                        
                        let videoOrImageModel = model!.imageOrVideoArray[index]
                        
                        playBtn.isHidden = sourceModel.dataType != 2
                        imgView.isHidden = false
                        if sourceModel.dataType == 2 {
                            imgView.image = sourceModel.videoCoverImage
                            playBtn.addBtnAction(action: { (_) in
                                
                                let videoModel = BHImageOrVideoModel.init()
                                videoModel.videoUrl = sourceModel.url
                                BHMediaTool.previewVideo(model: videoModel)
                                
                            }, for: .touchUpInside)
                        }else {
                            imgView.sd_setImage(with: URL.init(string: sourceModel.url), completed: nil)
                        }
                        imgView.addViewAction { (_) in
                            if sourceModel.dataType == 2    { // 视频
                                BHMediaTool.previewVideo(model: videoOrImageModel)
                            }else {
                                BHMediaTool.showPreview(array: self.model!.imageOrVideoArray, current: index )
                            }
                        }
                    }else {
                        playBtn.isHidden = true
                        imgView.isHidden = true
                    }
                }
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
